#


import time

import requests as requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from config import webdriverPath, hostUrl


class zyshgzbObj:
    i = 0
    item = None

    def set(self, item):
        self.i = item['currQuantity']
        self.list = []
        self.item = item
        self.citationList = []

    def run(self):
        # url_root = 'https://webofscience-clarivate-cn-s.era.lib.swjtu.edu.cn/wos/alldb/basic-search'
        # 创建ChromeOptions对象
        chrome_options = webdriver.ChromeOptions()
        # 禁止加载图片等资源
        chrome_options.add_argument("--disable-images")
        chrome_options.add_argument("--disable-plugins")
        chrome_options.add_argument("--disable-extensions")
        # 设置全屏参数
        chrome_options.add_argument("--start-maximized")

        # 创建WebDriver对象时传入ChromeOptions
        self.driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
        self.driver.get('https://www.mca.gov.cn')  # 打开的页面

    def getList(self):

        self.driver.get('https://www.mca.gov.cn/gdnps/pc/index.jsp?mtype=1')  # 打开的页面
        # 等待元素加载
        try:
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                  '#zcul > li:nth-child(2) > a'))
            )
            print("元素加载成功")
        except Exception as e:
            print("元素加载失败:", e)
            self.driver.quit()
            return
        self.driver.find_element(By.CSS_SELECTOR, '#zcul > li:nth-child(2) > a').click()
        time.sleep(2)
        # 获取元素
        elements = self.driver.find_elements(By.CSS_SELECTOR,
                                             '#infofromUl li')

        # 遍历元素
        for element in elements:
            try:
                # 获取元素的属性值
                a = element.find_element(By.CSS_SELECTOR, 'a')
                source = a.text
                if(self.list.__contains__(source)):
                    continue

                a.click()
                time.sleep(2)
                # 获取元素
                # 获取分页数量
                # 等待元素加载
                try:
                    # 或者等待直到某个元素可见
                    WebDriverWait(self.driver, 10).until(
                        EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                          '#rightDiv > div.page.txt14.fr.mar40 > span'))
                    )
                    print("元素加载成功")
                except Exception as e:
                    print("元素加载失败:", e)
                    self.driver.quit()
                    return
                page = self.driver.find_element(By.CSS_SELECTOR, '#rightDiv > div.page.txt14.fr.mar40 > span').text
                # 共15页/219条
                for i in range(1, int(page.split('页/')[0].replace('共', '')) + 1):

                    items = self.driver.find_elements(By.CSS_SELECTOR,
                                                      '#rightDiv > div.item > ul > li')
                    # 遍历元素
                    for i in items:
                        # 获取元素的属性值
                        a1 = i.find_element(By.CSS_SELECTOR, 'a')
                        title = a1.text
                        href = a1.get_attribute('href')
                        date = i.find_element(By.CSS_SELECTOR, 'span').text
                        print(source, title, href, date)
                        requests.post(hostUrl + '/policy/save',
                                      json={'documentName': title, 'accessionNo': href,
                                            'url': href, 'date': date + ' 00:00:00',
                                            'type': 'mca.gov',
                                            'source': source,
                                            'status': '10'})
                        # return
                    # 点击下一页
                    p1 = self.driver.find_elements(By.CSS_SELECTOR,
                                                   '#rightDiv > div.page.txt14.fr.mar40 > a')
                    p1[len(p1) - 2].click()
                    time.sleep(3)
                # 返回
            except Exception as e:
                print(e)
                continue


    def detail(self):
        while True:
            res = requests.post(hostUrl + '/policy/list', json={'type': 'mca.gov', })
            if res.json().get('code') != 200:
                break
            data = res.json().get('data')
            try:
                self.driver.get(data['url'])
                time.sleep(2)
                # 等待元素加载
                # 或者等待直到某个元素可见
                WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                      '#rightDiv'))
                )
                print("元素加载成功")

                # 获取元素
                fullText = self.driver.find_element(By.CSS_SELECTOR,
                                                    '#rightDiv').get_attribute(
                    'innerHTML')
                accessionNo = '无'
                try:
                    accessionNo = self.driver.find_element(By.CSS_SELECTOR,
                                                           '#syh').text
                    abstractInfo = self.driver.find_element(By.CSS_SELECTOR,
                                                            '#htmlContent > p:nth-child(1)').text
                    if len(abstractInfo) < 100:
                        abstractInfo = abstractInfo + '\n' + self.driver.find_element(By.CSS_SELECTOR,
                                                                                      '#htmlContent > p:nth-child(2)').text
                    if len(abstractInfo) < 100:
                        abstractInfo = abstractInfo + '\n' + self.driver.find_element(By.CSS_SELECTOR,
                                                                                  '#htmlContent > p:nth-child(3)').text
                except Exception as e:
                    print(e)
                    abstractInfo = '无'

                print(abstractInfo)
                data['accessionNo'] = accessionNo
                data['fullText'] = fullText
                data['abstractInfo'] = abstractInfo
                data['status'] = '20'
                requests.post(hostUrl + '/policy/save',
                              json=data)

            except Exception as e:
                data['status'] = '30'
                requests.post(hostUrl + '/policy/save',
                              json=data)
                print(e)


if __name__ == '__main__':
    a = zyshgzbObj()
    a.list = ['办公厅（国际合作司）', '政策法规司', '规划财务司', '社会组织管理局（社会组织执法监督局）', '社会救助司', '区划地名司', '老龄工作司', '社会事务司', '养老服务司', '儿童福利司', '慈善事业促进司', '机关党委（人事司）', '离退休干部局']
    a.run()
    # a.getList()
    a.detail()
    a.driver.quit()
    time.sleep(5)
